package conf

import (
	"fmt"
	"gopkg.in/ini.v1"
	"os"
)

//Global 全局变量的声明

var AppConf = new(AppConfig)

type AppConfig struct {
	EnvConf        `ini:"app"`
	MysqlReadConf  `ini:"mysql_read"`
	MysqlWriteConf `ini:"mysql_write"`
	RedisConf      `ini:"redis"`
	SmsConf        `ini:"sms"`
	PathConf       `ini:"path"`
	LogConf        `ini:"zydhlog"`
}

// EnvConf app 环境配置
type EnvConf struct {
	AppMode  string `ini:"app_mode"`
	HttpPort string `ini:"http_port"`
}

// MysqlReadConf 读mysql 配置
type MysqlReadConf struct {
	Name    string `ini:"name"`
	Host    string `ini:"host"`
	Port    int    `ini:"port"`
	User    string `ini:"user"`
	Psd     string `ini:"psd"`
	Dbname  string `ini:"dbname"`
	Charset string `ini:"charset"`
}

// MysqlWriteConf 写mysql 配置
type MysqlWriteConf struct {
	Name    string `ini:"name"`
	Host    string `ini:"host"`
	Port    int    `ini:"port"`
	User    string `ini:"user"`
	Psd     string `ini:"psd"`
	Dbname  string `ini:"dbname"`
	Charset string `ini:"charset"`
}

// RedisConf redis 配置
type RedisConf struct {
	Name string `ini:"name"`
	Host string `ini:"host"`
	Port int    `ini:"port"`
}

// SmsConf sms 短信发送配置
type SmsConf struct {
	Host     string `ini:"host"`
	Account  string `ini:"account"`
	Password string `ini:"password"`
	Userid   int    `ini:"userid"`
}

// PathConf 图片位置配置
type PathConf struct {
	Host            string `ini:"host"`
	UploadFilePath  string `ini:"upload_file_path"`
	GoodsImagesPath string `ini:"goods_images_path"`
	AvatarPath      string `ini:"avatar_path"`
}

// LogConf 日志配置
type LogConf struct {
	LogFilePath             string `ini:"log_file_path"`
	DebugMode               uint   `ini:"debug_mode"`
	LogToSite               uint   `ini:"log_to_site"`
	RuntimeGomaxprocs       int    `ini:"runtime_gomaxprocs"`
	LogChanMaxCap           uint   `ini:"log_chan_max_cap"`
	CutFileType             int    `ini:"cut_file_type"`
	LogShowTimeFormat       string `ini:"log_show_time_format"`
	CutFileByDateTimeFormat string `ini:"cut_file_by_date_time_format"`
	CutFileBySizeTimeFormat string `ini:"cut_file_by_size_time_format"`
	LogFileMaxSize          int    `ini:"log_file_max_size"`
	RuntimeCallerLayer      int    `ini:"runtime_caller_layer"`
	LogLevel                int    `ini:"log_level"`
}

// Init 映射加载
func Init() {
	//加载ini配置文件
	if err := ini.MapTo(AppConf, "./conf/conf.ini"); err != nil {
		fmt.Printf("Fail to read file: %v", err)
		os.Exit(1)
	}
}
